package com.jumi.microservice.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ExportExcelCommonResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.afterseal.*;
import com.jumi.microservice.enumerate.LogisticsStatusEnum;
import com.jumi.microservice.enumerate.PhoneTypeEnum;
import com.jumi.microservice.enumerate.SelfFlagEnum;
import com.jumi.microservice.enumerate.SupplierTypeEnum;
import com.jumi.microservice.service.admin.AdminExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author by mazhao
 * @Classname AdminListController
 * @Description TODO
 * @Date 2020/10/6 16:44
 */
@Api(tags = "售后订单excel导出导入相关接口")
@RestController
@RequestMapping("/afterSale")
public class AdminExcelController extends BaseController {

    @Resource
    AdminExcelService adminExcelService;

    @ApiOperation("售后所有订单导出")
    @PostMapping("/allList/export")
    public void allExcelExport(HttpServletResponse response, AfterSealAllRequest request) throws IOException {
//        request.checkRequest();
        List<Byte> status = new ArrayList<>();
        status.add(LogisticsStatusEnum.WAIT_SEND.getCode());
        status.add(LogisticsStatusEnum.WAIT_RECEIVE.getCode());
        status.add(LogisticsStatusEnum.CONFIRM_RECEIVE.getCode());
        AdminAfterSaleRequest adminAfterSaleRequest = new AdminAfterSaleRequest();
        BeanUtils.copyProperties(request, adminAfterSaleRequest);
        if (request.getType() != null && request.getType().equals(SupplierTypeEnum.DELIVERY_WAREHOUSE.getCode())) {
            adminAfterSaleRequest.setWarehouseId(request.getWarehouse());
        } else if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER.getCode())) {
            adminAfterSaleRequest.setSupplierId(request.getWarehouse());
        }
        if (request.getPhoneType() != null && request.getPhoneType().equals(PhoneTypeEnum.BUYER.getCode())) {
            adminAfterSaleRequest.setBuyerPhone(request.getPhone());
        } else if (request.getPhoneType() != null && request.getPhoneType().equals(PhoneTypeEnum.SELLER.getCode())) {
            adminAfterSaleRequest.setReceiverPhone(request.getPhone());
        }
        ExportExcelCommonResponse<AfterSaleGoodsInfo> excelExportExportExcelCommonResponse = adminExcelService
                .excelExport(adminAfterSaleRequest, "AfterSaleAll.xlsx", status, SelfFlagEnum.YES_SELF.getCode(), SelfFlagEnum.NO_SELF.getCode());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelExportExportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), AfterSaleGoodsInfoE.class).sheet("sheet1").doWrite(excelExportExportExcelCommonResponse.getData());
    }

    @ApiOperation("售后自营订单导出")
    @PostMapping("/selfWaitSendList/export")
    public void selfExcelExport(HttpServletResponse response, AfterSealSelfRequest request) throws IOException {
//        request.checkRequest();
        List<Byte> status = new ArrayList<>();
        status.add(LogisticsStatusEnum.WAIT_SEND.getCode());
        AdminAfterSaleRequest adminAfterSaleRequest = new AdminAfterSaleRequest();
        BeanUtils.copyProperties(request, adminAfterSaleRequest);
        adminAfterSaleRequest.setWarehouseId(request.getWarehouse());
        ExportExcelCommonResponse<AfterSaleGoodsInfo> excelExportExportExcelCommonResponse = adminExcelService
                .excelExport(adminAfterSaleRequest, "AfterSaleSelfWaitSend.xlsx", status, SelfFlagEnum.YES_SELF.getCode());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelExportExportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), AfterSaleGoodsInfo.class).sheet("sheet1").doWrite(excelExportExportExcelCommonResponse.getData());
    }

    @ApiOperation("售后非自营订单导出")
    @PostMapping("/noSelfWaitSendList/export")
    public void noSelfExcelExport(HttpServletResponse response, AfterSealNoSelfRequest request) throws IOException {
//        request.checkRequest();
        List<Byte> status = new ArrayList<>();
        status.add(LogisticsStatusEnum.WAIT_SEND.getCode());
        AdminAfterSaleRequest adminAfterSaleRequest = new AdminAfterSaleRequest();
        BeanUtils.copyProperties(request, adminAfterSaleRequest);
        ExportExcelCommonResponse<AfterSaleGoodsInfo> excelExportExportExcelCommonResponse = adminExcelService
                .excelExport(adminAfterSaleRequest, "AfterSaleNoSelfWaitSend.xlsx", status, SelfFlagEnum.NO_SELF.getCode());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelExportExportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), AfterSaleGoodsInfo.class).sheet("sheet1").doWrite(excelExportExportExcelCommonResponse.getData());
    }

    @ApiOperation("售后待收货订单导出")
    @PostMapping("/WaitReceiveList/export")
    public void waitReceiveExcelExport(HttpServletResponse response, AfterSealGoodsReceivedRequest request) throws IOException {
//        request.checkRequest();
        List<Byte> status = new ArrayList<>();
        status.add(LogisticsStatusEnum.WAIT_RECEIVE.getCode());
        AdminAfterSaleRequest adminAfterSaleRequest = new AdminAfterSaleRequest();
        BeanUtils.copyProperties(request, adminAfterSaleRequest);
        if (request.getType() != null && request.getType().equals(SupplierTypeEnum.DELIVERY_WAREHOUSE.getCode())) {
            adminAfterSaleRequest.setWarehouseId(request.getWarehouse());
        } else if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER.getCode())) {
            adminAfterSaleRequest.setSupplierId(request.getWarehouse());
        }
        ExportExcelCommonResponse<AfterSaleGoodsInfo> excelExportExportExcelCommonResponse = adminExcelService
                .excelExport(adminAfterSaleRequest, "AfterSaleWaitReceive.xlsx", status, SelfFlagEnum.NO_SELF.getCode(), SelfFlagEnum.YES_SELF.getCode());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelExportExportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), AfterSaleGoodsInfoE.class).sheet("sheet1").doWrite(excelExportExportExcelCommonResponse.getData());
    }

    @ApiOperation("售后已完成订单导出")
    @PostMapping("/finishedList/export")
    public void finishedExcelExport(HttpServletResponse response, AfterSealGoodsReceivedRequest request) throws IOException {
//        request.checkRequest();
        List<Byte> status = new ArrayList<>();
        status.add(LogisticsStatusEnum.CONFIRM_RECEIVE.getCode());
        AdminAfterSaleRequest adminAfterSaleRequest = new AdminAfterSaleRequest();
        BeanUtils.copyProperties(request, adminAfterSaleRequest);
        if (request.getType() != null && request.getType().equals(SupplierTypeEnum.DELIVERY_WAREHOUSE.getCode())) {
            adminAfterSaleRequest.setWarehouseId(request.getWarehouse());
        } else if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER.getCode())) {
            adminAfterSaleRequest.setSupplierId(request.getWarehouse());
        }
        ExportExcelCommonResponse<AfterSaleGoodsInfo> excelExportExportExcelCommonResponse = adminExcelService
                .excelExport(adminAfterSaleRequest, "AfterSaleFinished.xlsx", status, SelfFlagEnum.NO_SELF.getCode(), SelfFlagEnum.YES_SELF.getCode());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelExportExportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), AfterSaleGoodsInfoE.class).sheet("sheet1").doWrite(excelExportExportExcelCommonResponse.getData());
    }

    @ApiOperation("售后自营待发货导入")
    @PostMapping("/selfWaitSend/import")
    public ResponseResult<List<String>> selfWaitSendImport(HttpServletRequest request) throws IOException {
        Long uid = getUid();
        String name = getUserName();
        return success(adminExcelService.sendExcelImport(request, uid, name));
    }

    @ApiOperation("售后非自营待发货导入")
    @PostMapping("/noSelfWaitSend/import")
    public ResponseResult<List<String>> noSelfWaitSendImport(HttpServletRequest request) throws IOException {
        Long uid = getUid();
        String name = getUserName();
        return success(adminExcelService.sendExcelImport(request, uid, name));
    }
}
